package com.czl.module_storehouse.activity.intostore.saveloc;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.base.BaseOperationLocationActivity;
import com.czl.module_storehouse.adapter.SaveStorageLocAdapter;
import com.czl.module_storehouse.databinding.ActivitySaveLocBinding;
import com.czl.module_storehouse.event.SaveLocationEvent;
import com.czl.module_storehouse.event.SelectedProductResultEvent;
import com.czl.module_storehouse.mvp.presenter.StorehouseLocationPresenter;
import com.czl.module_storehouse.mvp.view.StorehouseLocationView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 存放位置
 *
 * @author xubin
 */

public class SaveLocActivity extends BaseOperationLocationActivity<ActivitySaveLocBinding> implements StorehouseLocationView {

    private SaveStorageLocAdapter mAdapter;
    private SortBean mSortBean;
    private int mMaxNum;
    @InjectPresenter
    protected StorehouseLocationPresenter mStorehouseLocationPresenter;

    @Override
    protected ActivitySaveLocBinding getBinding(LayoutInflater layoutInflater) {
        return ActivitySaveLocBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        mToolBinding.toolbarContentTitle.setText("存放位置");
        mAdapter = new SaveStorageLocAdapter(R.layout.item_save_loc, new ArrayList<>(), mSelectedSet);
        binding.recyclerView.setAdapter(mAdapter);
        binding.llCommonAdd.tvAddGoods.setText("选择库位");
        binding.btnConfirm.setOnClickListener(this);
        binding.llCommonAdd.llAdd.setOnClickListener(this);
        if (mSortBean == null) {
            return;
        }
        List<LocatListBean> locatList = mSortBean.getLocatList();
        if (locatList != null) {
            for (LocatListBean bean : locatList) {
                mSelectedSet.add(bean.getStorehouseLocatId());
            }
            mAdapter.addData(locatList);
        }
        mAdapter.setMaxCount(mMaxNum);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(SaveLocationEvent event) {
        if (event == null) {
            return;
        }
        mSortBean = event.getSortBean();
        mMaxNum = event.getMaxNum();
    }

    @Override
    protected void loadData() {
        super.loadData();
        getStorehouseLocatList();
    }

    @Override
    protected StorehouseLocationPresenter getLocationPresenter() {
        return mStorehouseLocationPresenter;
    }

    @Override
    protected boolean callBackModel(LocatListBean bean) {
        boolean call = super.callBackModel(bean);
        if (!call) {
            showToast("该库位已添加");
            return false;
        }
        if (bean == null) {
            return false;
        }
        bean.setDefaultNumType(LocatListBean.NUM_TYPE_STOCK);
//        List<LocatListBean> data = mAdapter.getData();
//        if (!LocatListBean.hasExists(data, bean)) {
//        }
        mAdapter.addData(bean);
        return true;
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        if (id == R.id.ll_add) {
            getStorehouseLocatList();
        } else if (id == R.id.btn_confirm) {
            submit();
        }
    }

    protected void submit() {
        List<LocatListBean> data = mAdapter.getData();
        mSortBean.setLocatList(data);
        EventBus.getDefault().postSticky(new SelectedProductResultEvent());
        finish();
    }
}
